﻿/*global angular*/
/*jslint unparam:true*/
(function () {
    'use strict';
    var app = angular.module('app');
    app.factory('personService', ['$http', '$q', function ($http, $q) {
        function search(filter, ss) {
            var deferred = $q.defer();
            
            $http.get('../api/Persons/GetByFilter', {
                params:
                    {
                        filter: angular.fromJson(filter),
                        ss: ss
                    }
            }).
                success(function (data) {//, status, headers, config
                    deferred.resolve(data);
                }).
                error(function (data, status) {//, headers, config
                    deferred.reject(status);
                });
            return deferred.promise;
        }
        //todo add local storage
        function getById(personId) {
            var deferred = $q.defer();

            $http.get('../api/Persons/GetById', {
                params:
                    {
                        id: personId
                    }
            }).
                success(function (data) {//, status, headers, config
                    deferred.resolve(data);
                }).
                error(function (data, status) {//, headers, config
                    deferred.reject(status);
                });
            return deferred.promise;
        }
        
        
        // Define the functions and properties to reveal.
        var service = {
           // getDefinition: getDefinition,
            search: search,
            getById: getById
        };

        return service;
    }]);
}());